package com.sunday.pinxinghui.model.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.network.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by AnyOne on 2018/4/9.
 */

public class WithDrawActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.et_money)
    ClearEditText etMoney;
    @Bind(R.id.et_ali_account)
    ClearEditText etAliAccount;
    @Bind(R.id.btn_withdraw)
    TextView btnWithdraw;

    private Double reward;
    private long memberId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.bind(this);
        memberId = BaseApplication.getInstance().getMember().getId();
        reward = getIntent().getDoubleExtra("reward", 0);
        etMoney.setHint("最多可提现" + reward.toString());

    }

    @OnClick(R.id.btn_withdraw)
    void withDraw() {
        String zfb = etAliAccount.getText().toString().trim();
        String withDrawMoney = etMoney.getText().toString().trim();
        if (TextUtils.isEmpty(zfb) || TextUtils.isEmpty(withDrawMoney)) {
            ToastUtils.showToast(mContext, "请填写完整信息");
            return;
        }
        if (Double.valueOf(withDrawMoney).compareTo(reward) > 0 || Double.valueOf(withDrawMoney) <= 0) {
            ToastUtils.showToast(mContext, "请输入正确金额");
            return;
        }

        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().withdraw(memberId, zfb, withDrawMoney);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "提现成功");
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
            }
        });
    }
}
